package csc.fresher.view;

import java.io.IOException;
import java.io.Serializable;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;

import javax.annotation.PostConstruct;
import javax.annotation.PreDestroy;
import javax.faces.context.ExternalContext;
import javax.faces.context.FacesContext;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Scope;
import org.springframework.stereotype.Component;

import csc.fresher.entity.Customer;
import csc.fresher.entity.CustomerAccount;
import csc.fresher.entity.SavingAccount;
import csc.fresher.entity.SystemUser;
import csc.fresher.entity.Transaction;
import csc.fresher.service.SavingAccountService;
import csc.fresher.service.SystemUserService;

@Component
@Scope("session")
public class SavingAccManagedBean implements Serializable {

	/**
	 * 
	 */
	private static final long serialVersionUID = 1L;

	@Autowired
	SavingAccountService savingAccountService;

	@Autowired
	SystemLoginManageBean systemLoginManageBean;

	@Autowired
	private SystemUserService systemUserService;

	private String accountNumber;

	private String idCardNumber;

	private String message = "";

	private Customer customer;

	private SystemUser systemUser = null;

	private List<CustomerAccount> cusAccounts;

	private CustomerAccount selectedAccount;

	private List<Long> roles = null;

	private boolean support = false;

	private boolean admin = false;

	public boolean isSupport() {
		if (roles != null) {
			if (roles.contains(new Long(4))) {
				support = true;
			}
		}
		return support;
	}

	public boolean isAdmin() {
		if (roles != null) {
			if (roles.contains(new Long(3))) {
				admin = true;
			}
		}
		return admin;
	}

	public List<Long> getRoles() {
		return roles;
	}

	public void setRoles(List<Long> roles) {
		this.roles = roles;
	}

	public CustomerAccount getSelectedAccount() {
		return selectedAccount;
	}

	public void setSelectedAccount(CustomerAccount selectedAccount) {
		this.selectedAccount = selectedAccount;
	}

	public SystemUser getSystemUser() {
		return systemUser;
	}

	public void setSystemUser(SystemUser systemUser) {
		this.systemUser = systemUser;
	}

	public SavingAccManagedBean() {
	}

	public Customer getCustomer() {
		return customer;
	}

	public void setCustomer(Customer customer) {
		this.customer = customer;
	}

	public String getMessage() {
		return message;
	}

	public void setMessage(String message) {
		this.message = message;
	}

	public List<CustomerAccount> getCusAccounts() {
		return cusAccounts;
	}

	public void setCusAccounts(List<CustomerAccount> cusAccounts) {
		this.cusAccounts = cusAccounts;
	}

	public String getAccountNumber() {
		return accountNumber;
	}

	public void setAccountNumber(String accountNumber) {
		this.accountNumber = accountNumber;
	}

	public String getIdCardNumber() {
		return idCardNumber;
	}

	public void setIdCardNumber(String idCardNumber) {
		this.idCardNumber = idCardNumber;
	}

	public String selectAccount() {
		return "savingAccView";
	}

	public void refresh() {
		customer = null;
		cusAccounts = null;
		accountNumber = "";
		idCardNumber = "";
		message = "";
	}

	public void search() {
		if (savingAccountService != null) {
			cusAccounts = new ArrayList<>();
			customer = null;
			if (idCardNumber.equals("") && accountNumber.equals("")) {
				message = "Please enter account number or ID card number you want to search.";
			} else {
				if (!idCardNumber.equals("")) {
					long id = Long.parseLong(idCardNumber);
					cusAccounts = savingAccountService
							.getListCustomerAccountByIDCardNumber(id);
					if (cusAccounts != null && cusAccounts.size() > 0) {
						CustomerAccount customerAccount = cusAccounts.get(0);
						customer = savingAccountService
								.getCustomerByCustomerID(customerAccount
										.getCustomerID());
					}
				}
				if ((cusAccounts == null || cusAccounts.size() == 0)
						&& !accountNumber.equals("")) {
					long accNumber = Long.parseLong(accountNumber);
					CustomerAccount customerAccount = savingAccountService
							.getCustomerAccountByID(accNumber);
					if (customerAccount != null) {
						cusAccounts.add(customerAccount);
						customer = savingAccountService
								.getCustomerByCustomerID(customerAccount
										.getCustomerID());
					}
				}
				if (cusAccounts == null || cusAccounts.size() == 0) {
					message = "Not found any account";
				}
			}
		} else {
			message = "Error";
		}
	}

	@PostConstruct
	public void init() {
		if (systemLoginManageBean != null) {
			systemUser = systemLoginManageBean.getSystemUser();
			if (systemUser != null) {
				System.out.println(systemUser.getFirstName());
				roles = systemUserService.getRoleByUserName(systemUser
						.getUserName());
			} else {
				redirect("templates3/Logon.jsf");
			}
		} else {
			System.out.println("System login manage bean is null");

		}
	}

	private void redirect(String url) {
		ExternalContext externalContext = FacesContext.getCurrentInstance()
				.getExternalContext();
		try {
			externalContext.redirect(url);
		} catch (IOException e) {
			e.printStackTrace();
		}
	}

	private void forward(String url) {
		try {
			FacesContext.getCurrentInstance().getExternalContext()
					.dispatch(url);
		} catch (IOException e) {
			e.printStackTrace();
		}
	}

}
